<template>
	<view class="agentShare">
		<!-- <web-view :webview-styles="webviewStyles" :src="activityUrl" @message="onWebViewPost"></web-view> -->
		<!-- <view class="text-lg text-center">{{ activityInfo.acName || '' }}</view>
		<view class="text-grey text-sm text-center">{{ activityInfo.acDetail }}</view> -->
		<image class="activityImg" src="http://app.aptenon.com/crm/PlayBill/618back.png" mode="widthFix"></image>
		<!-- <image class="activityImg" src="http://app.aptenon.com/crm/PlayBill/HB_3.jpg" mode="widthFix"></image> -->
	</view>
</template>

<script>
/*
	url参数：
	pid 海报id
	sid 分享id
	aid 活动id
	scene 扫码进来的参数，需要uri decode	
	*/
import { mapActions } from 'vuex';
import logger from '../../utils/log.js';
let timer = null;
export default {
	onLoad(obj) {
		console.log(obj);
		let self = this;
		let pid = obj.pid;
		let sid = obj.sid;
		//#ifdef MP-WEIXIN
		uni.showLoading({
			title: '加载中',
			mask: true
		});
		if (obj.scene) {
			let scene = decodeURIComponent(obj.scene);
			try {
				let arr = scene.split('&');
				let cacheObj = {};
				arr.forEach((e, i) => {
					let str = e.split('=');
					cacheObj[str[0]] = str[1];
				});
				if (cacheObj.pid) {
					pid = cacheObj.pid;
				}
				if (cacheObj.sid) {
					sid = cacheObj.sid;
				}
			} catch (e) {
				//TODO handle the exception
				uni.showModal({
					title: '出错了',
					content: '获取页面参数失败',
					showCancel: false
				});
			}
		}
		if (pid || sid) {
			if (this.$store.state.user.userInfo.openId) {
				self.getData(pid, sid);
			} else {
				uni.$once('getedOpenId', () => {
					self.getData(pid, sid);
				});
			}
		}
		if (obj.aid) {
			this.initDataByAid(obj.aid);
		}
		//#endif
	},
	data() {
		return {
			paintPallette: {},
			modalVisible: false,
			tempFilePath: null,
			webviewStyles: {},
			activityUrl: null,
			shareId: null,
			postId: null,
			activityInfo: {}
		};
	},
	onReady() {
		timer = new Date();
	},
	// onShareAppMessage(obj) {
	// 	let title = this.activityInfo.acName || '';
	// 	let url = '';
	// 	if (this.postId) {
	// 		url = `/activityPages/agentActivity/agentActivityShare?pid=${this.postId}`;
	// 	}
	// 	if (this.shareId) {
	// 		url = `/activityPages/agentActivity/agentActivityShare?sid=${this.shareId}`;
	// 	}
	// 	return {
	// 		title: title,
	// 		path: url
	// 	};
	// },
	methods: {
		...mapActions({
			shareActivity: 'activity/shareActivity',
			getActivityInfoByPid: 'activity/getActivityInfoByPid',
			getActivityInfoBySid: 'activity/getActivityInfoBySid',
			getActivityInfoByAid: 'activity/getActivityInfoByAid'
		}),
		onWebViewPost(res) {
			console.log(res);
		},
		getData(pid, sid) {
			let self = this;
			if (pid) {
				// 海报id
				self.initDataByPost(pid);
				try {
					logger.info(
						JSON.stringify({
							openId: self.$store.state.user.userInfo.openId,
							pid
						})
					);
				} catch (e) {
					//TODO handle the exception
				}
			} else if (sid) {
				// 分享id
				self.initDataByShare(sid);
				try {
					logger.info(
						JSON.stringify({
							openId: self.$store.state.user.userInfo.openId,
							sid
						})
					);
				} catch (e) {
					//TODO handle the exception
				}
			}
		},
		initDataByAid(aid) {
			this.getActivityInfoByAid({
				acId: aid,
				sourceOpenId: this.$store.state.user.userInfo.openId
			})
				.then(res => {
					uni.hideLoading();
					console.log(res);
					if (res && res.code == '000000') {
						this.activityInfo = res.data;
						if (res.data.pbId) {
							this.postId = this.pbId;
						} else {
							//#ifdef MP-WEIXIN
							wx.hideShareMenu();
							// uni.showModal({
							// 	title: '提示',
							// 	content: '您尚未生成海报，如要分享活动，请先生成海报',
							// 	showCancel: false
							// });
							//#endif
						}
					}
				})
				.catch(err => {
					uni.hideLoading();
					console.log(err);
				});
		},
		initDataByPost(pid) {
			this.postId = pid;
			this.getActivityInfoByPid({
				pbId: pid
			})
				.then(res => {
					console.log(res);
					if (res && res.code == '000000') {
						let curUserOpenId = this.$store.state.user.userInfo.openId;
						let pbSourceOpenid = res.data.pbSourceOpenid;
						this.activityInfo = res.data;
						if (curUserOpenId == pbSourceOpenid) {
							//海报发起人的openid和当前用户openid一样
							return;
						}
						return
						// return this.shareActivity({
						// 	shareOpenId: pbSourceOpenid || '',
						// 	toShareOpenId: curUserOpenId,
						// 	pbId: pid
						// });
					}
				})
				.then(res => {
					uni.hideLoading();
					console.log(res);
					if (res && res.code == '000000') {
						this.shareId = res.id;
					}
				})
				.catch(err => {
					uni.hideLoading();
					console.log(err);
				});
		},
		initDataByShare(sid) {
			this.shareId = sid;
			this.getActivityInfoBySid({
				shareId: sid
			})
				.then(res => {
					console.log(res);
					let curUserOpenId = this.$store.state.user.userInfo.openId;
					let srShareOpenid = res.data.srShareOpenid;
					this.activityInfo = res.data;
					let srToShareOpenid = res.data.srToShareOpenid;
					if (curUserOpenId == srToShareOpenid) {
						//当前用户的openid等于被分享人
						return;
					}
					return
					// return this.shareActivity({
					// 	shareOpenId: srToShareOpenid || '',
					// 	toShareOpenId: curUserOpenId,
					// 	pbId: res.data.pbId
					// });
				})
				.then(res => {
					uni.hideLoading();
					console.log(res);
					if (res && res.code == '000000') {
						this.shareId = res.id;
					}
				})
				.catch(err => {
					uni.hideLoading();
					console.log(err);
				});
		}
	}
};
</script>

<style lang="less">
.agentShare {
	.btnContainer {
		margin: 100rpx auto;
		text-align: center;
	}
	.modalBtnContainer {
		margin: 12rpx 0;
		text-align: center;
	}
	.activityImg{
		width: 100vw;
		// height: 100vh;
	}
}
</style>
